package com.shfl.servermanage.logic.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.shfl.servermanage.exception.ApiException;
import com.shfl.servermanage.logic.bean.SysServerBt;
import com.shfl.servermanage.logic.mapper.SysServerBtMapper;
import com.shfl.servermanage.logic.model.info.BtUserInfo;
import com.shfl.servermanage.logic.model.request.EditServerBtRequest;
import com.shfl.servermanage.logic.model.response.ServerBtDetailResponse;
import com.shfl.servermanage.logic.service.SysServerBtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class SysServerBtServiceImpl implements SysServerBtService {

    @Autowired
    private SysServerBtMapper sysServerBtMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(SysServerBt record) {
        return sysServerBtMapper.insertSelective(record);
    }

    @Override
    public void findByServerId(ServerBtDetailResponse serverBtDetailResponse, Long serverId) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("serverId", serverId);
        List<SysServerBt> sysServerBtList = sysServerBtMapper.findListByMap(objectMap);
        if (!CollectionUtils.isEmpty(sysServerBtList)) {
            SysServerBt sysServerBt = sysServerBtList.get(0);
            BeanUtil.copyProperties(sysServerBt, serverBtDetailResponse);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(EditServerBtRequest editServerBtRequest) {
        SysServerBt sysServerBt = sysServerBtMapper.selectByPrimaryKey(editServerBtRequest.getId());
        if (sysServerBt == null || !Objects.equals(sysServerBt.getServiceId(), editServerBtRequest.getServerId())) {
            throw new ApiException("1204");
        }
        SysServerBt updateSysServerBt = new SysServerBt();
        BeanUtil.copyProperties(editServerBtRequest, updateSysServerBt);
        sysServerBtMapper.updateByPrimaryKeySelective(updateSysServerBt);
    }

    @Override
    public BtUserInfo findBtUserInfo(Long serverId) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("serverId", serverId);
        List<SysServerBt> sysServerBtList = sysServerBtMapper.findListByMap(objectMap);
        if (CollectionUtil.isEmpty(sysServerBtList)) {
            throw new ApiException("1204");
        }
        SysServerBt sysServerBt = sysServerBtList.get(0);
        BtUserInfo btUserInfo = new BtUserInfo();
        btUserInfo.setAddress(sysServerBt.getBtUrl());
        btUserInfo.setSecretKey(sysServerBt.getBtSign());
        btUserInfo.setRootDirectory(sysServerBt.getRootDirectory());
        return btUserInfo;
    }
}
